﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using IGolovkine.Core.Linq;
using IGolovkine.Core.Collections.Specialized;
using System.Threading;
using System.Windows.Threading;

namespace IGolovkine.ShoutCastRipper.UI.AdminPanel
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class AdminPanelWindow : Window
    {
        private AdminPanelWindowController _controller;
        public AdminPanelWindow()
        {
            _controller = new AdminPanelWindowController(this.Dispatcher);
            InitializeComponent();
            _statusListView.ItemsSource = _controller.Model.StatusItems;
            _statusListView.SelectionChanged += new SelectionChangedEventHandler(StatusListViewSelectionChanged);
            _statusBar.DataContext = _controller.Model.Status;
        }

        private void StatusListViewSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            IEnumerable<StationRipperStatusItem> items = e.AddedItems.Cast<StationRipperStatusItem>();
            IEnumerable<StationRipperEventItem> events = _controller.Model.EventItems.Where(evt=> items.Count(status=> status.Name == evt.SourceName) != 0);
            _eventListView.ItemsSource = new ObservableList<StationRipperEventItem>(events);
        }

        private void Execute(Action action)
        {
            try
            {
                action();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Admin Panel", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void StatusListViewCommandExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            this.Execute(() =>
            {
                DoEvents();
                ListView listView = (ListView)e.Source;
                IEnumerable<StationRipperStatusItem> items = listView.SelectedItems.Cast<StationRipperStatusItem>();
                if (e.Command == AdminPanelCommands.SkipCurrentTitle)
                {
                    items.ForEach(i => _controller.SkipCurrentTitleClick(i.Name));
                }
                else if (e.Command == AdminPanelCommands.StopStationRipper)
                {
                    items.ForEach(i => _controller.Stop(i.Name));
                }
                else if (e.Command == AdminPanelCommands.StartStationRipper)
                {
                    items.ForEach(i => _controller.Start(i.Name));
                }
            });
        }

        private void ApplicationCommandExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            if (e.Command == ApplicationCommands.Close)
            {
                if (MessageBox.Show("Close the application?", "Admin Panel", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    this.Close();
                }
            }
        }

        public static void DoEvents()
        {
            Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new ThreadStart(delegate { }));
        }
    }
}
